﻿using DeHeng_Mes.Constant;
using DeHeng_Mes.Model.Boss;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace DeHeng_Mes.Utils
{
    public static class PutMes
    {

        static CancellationTokenSource cts = new CancellationTokenSource();

        public static async Task<BossMessager> LineMesToMes(object bean, string produceHourUrl, string task)
        {
            BossMessager bossMessager = null;
            try
            {
                // 调用接口给工厂Mes推送数据
                bossMessager = JsonSerializer.Deserialize<BossMessager>(await HttpUtils.HttpRequestAsync(produceHourUrl, await MesLoginHelper.GetDhTokenAsync(cts.Token), HttpRequestType.POST, bean, cts.Token));
                // 解析返回结果
                Tools.ProduceLogText($"{task}成功，响应码：{bossMessager.code}，响应消息为：{bossMessager.msg}");
            }
            catch (JsonException ex)
            {
                Tools.ProduceLogTextError($"{task}返回数据解析失败，原因是{ex.Message}");
            }
            catch (Exception ex)
            {
                Tools.ProduceLogTextError($"{task}失败，原因是{ex.Message}");
            }

            return bossMessager;
        }
    }
}
